{% extends 'layout/manage.html' %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugins/editor-md/css/editormd.preview.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        .content div {
            text-align: center;
            margin-top: 50px;
        }

        .content div a {
            color: #2e6da4;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .title-list li {
            padding-left: 1px;
        }

        .title-list li a {
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i>wiki文档
                </div>
                <div class="function">
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'wiki_add' project_id=request.bug_platform.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                    </a>

                    {% if wiki_object %}
                        <a type="button" class="btn btn-primary btn-xs"
                           href="{% url 'wiki_edit' project_id=request.bug_platform.project.id wiki_id=wiki_object.id %}">
                            <i class="fa fa-edit" aria-hidden="true"></i> 编辑
                        </a>
                        <a type="button" class="btn btn-danger btn-xs"
                           href="{% url 'wiki_delete' project_id=request.bug_platform.project.id wiki_id=wiki_object.id %}">
                            <i class="fa fa-train" aria-hidden="true"></i> 删除
                        </a>
                    {% endif %}

                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">

                    <ul id="catalog" style="padding-left: 0">
                        <!--
                        <li id='1'><a href="#">老男孩</a></li>
                        <li id='2'><a href="#">中男孩</a>
                            <ul>
                                <li id='4'><a href="#">中中男孩</a></li>
                            </ul>
                        </li>
                        <li id='3'><a href="#">小男孩</a>
                            <ul>
                                <li id='5'><a href="#">小小男孩</a></li>
                            </ul>
                        </li>
                        -->
                    </ul>

                </div>
                <div class="col-sm-9 content">
                    {% if wiki_object %}
                        {#                        有值就把内容展示放这#}
                        <div id="previewMarkdown">
                            <textarea>{{ wiki_object.content }}</textarea>
                        </div>
                    {% else %}
                        {#                        没有值就显示新建#}
                        <div>
                            <h4>《{{ request.bug_platform.project.name }}》wiki文档库</h4>
                            <a href="{% url 'wiki_add' project_id=request.bug_platform.project.id %}">
                                <i class="fa fa-plus-circle" aria-hidden="true"></i>新建文章
                            </a>
                        </div>
                    {% endif %}

                </div>
            </div>
        </div>

    </div>
{% endblock %}
{% block js %}
    <script src="{% static 'plugins/editor-md/editormd.amd.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/jquery.flowchart.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugins/editor-md/lib/underscore.min.js' %}"></script>

    <script>

        //  http://127.0.0.1:8000/manage/3/wiki/detail/
        var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.bug_platform.project.id %}"
        $(function () {
            initCatalog();
            initPreviewMarkdown();
        });

        function initCatalog() {
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.bug_platform.project.id %}',
                type: 'GET',
                dataType: 'JSON',
                success: function (res) {
                    console.log(res)
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            // item = [1, '老男孩', null], [2, '中男孩', null], ... [5, '中中男孩', 2] values_list
                            // item = {id: 1, title: '老男孩', parent_id: null}, {id: 5, title: '中中男孩', parent_id: 2}  values
                            //item.id
                            //item.title
                            //item.parent_id
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                            var li = $("<li>").attr('id', 'id_' + item.id).append($("<a>").text(item.title).attr('href', href)).append("<ul>");
                            if (!item.parent_id) {
                                // 没有父级  <li id='1'><a href="#">老男孩</a></li>
                                // 添加到catalog【 创建li标签有a和ul
                                {#var li = $("<li>").attr('id', 'id_' + item.id).append($("<a>").text(item.title)).append("<ul>");#}
                                $("#catalog").append(li);
                            } else {
                                // 根据id_关联表的父级id找到下面的ul添加li
                                $("#id_" + item.parent_id).children('ul').append(li)
                            }
                        });
                    } else {
                        alert('失败');
                    }
                }
            });
        }

        function initPreviewMarkdown(){
            editormd.markdownToHTML('previewMarkdown', {
                // 遇见这些会有可能识别为标签，这里就是把这些都变成字符串
                htmlDecode: "style,script,iframe"
            });
        }

    </script>
{% endblock %}

